package com.yf.catcafe.service.impl;

import com.yf.catcafe.mapper.BooksMapper;
import com.yf.catcafe.mapper.GoodsMapper;
import com.yf.catcafe.mapper.UserMapper;
import com.yf.catcafe.pojo.dto.BookingDto;
import com.yf.catcafe.pojo.dto.SeckillCatDto;
import com.yf.catcafe.pojo.enity.Booking;
import com.yf.catcafe.pojo.enity.Cat;
import com.yf.catcafe.pojo.enity.User;
import com.yf.catcafe.service.BooksService;
import com.yf.catcafe.service.GoodsService;
import com.yf.catcafe.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;

@Service
public class BooksServiceImpl implements BooksService {

    @Autowired
    private BooksMapper booksMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void book(BookingDto bookingDto) {
        Booking booking=new Booking();
        Long id = bookingDto.getUserId();
        User user=userService.getById(id);
        BeanUtils.copyProperties(bookingDto,booking);
        booking.setObookingTime(LocalDateTime.now());
        booking.setUserNickname(user.getNickName());
        booking.setCatName(bookingDto.getCatName());
        booksMapper.insert(booking);
    }

    @Override
    public List<Booking> query() {
        List<Booking> bookings=booksMapper.query();
        return bookings;
    }

    @Override
    public void delete(Long id) {
        booksMapper.delete(id);
    }

    @Override
    public void seckillInsert(SeckillCatDto seckillCatDto) {
        Long catId = seckillCatDto.getCatId();
        Cat cat = goodsMapper.getById(catId);
        User user = userMapper.getById(seckillCatDto.getUserId());
        Booking booking=new Booking();
        LocalDateTime bookingTime = Instant.parse(seckillCatDto.getReserveTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        booking.setBookingTime(bookingTime);
        booking.setUserId(seckillCatDto.getUserId());
        booking.setCatName(cat.getName());
        booking.setObookingTime(LocalDateTime.now());
        booking.setUserNickname(user.getNickName());
        booking.setRemark("无");
        booksMapper.seckillInsert(booking);
    }
}
